<?php
class EndOfMeetingAction extends CAction {
	public function run() {
		$category = "EndOfMeetingAction [P#".$_GET['project_id']." M#".$_GET['id']."]";
		Yii::log("Loading meeting #".$_GET['id'],CLogger::LEVEL_INFO, $category);
		$this->controller->layout = 'empty';
		$meeting = Meetings::model()->findByPk($_GET['id']);
		if ($meeting == null) {
			throw new CHttpException(404, "Meeting #".$_GET['id']." not found");
		}
		$this->controller->project = $meeting->project;
		$this->controller->pageTitle = $this->controller->project;

		$update = false;

		if (isset($_POST['next_button']) || isset($_POST['prev_button'])) {
			$update = true;
			if (isset($_POST['Meetings'])) {
				if ($_POST['Meetings']['next_date'] == "") {
					unset($_POST['Meetings']['next_date']);
				} else {
					$_POST['Meetings']['next_date'] = DateUtils::parse(Constants::YII_DISPLAY_DATETIME_FORMAT, trim($_POST['Meetings']['next_date']).':00');
				}
				$meeting->attributes = $_POST['Meetings'];
				
				$update = $meeting->save();
			}
		}

		if (!$update) {
			Yii::log("Render.", CLogger::LEVEL_INFO, $category);
			$this->controller->render('end_of_meeting', array (
				'model' => $meeting
			));
		} else if (isset($_POST['prev_button'])) {
			$this->controller->redirect(array_merge(array('itemTree'), $_GET));
		} else {
			Yii::app()->user->setFlash('meeting', Yii::t('project', 'Meeting\'s informations have been saved.'));
			$this->controller->layout = 'project';
			Yii::log("Render.", CLogger::LEVEL_INFO, $category);
			$this->controller->render('end_of_meeting', array (
				'model' => $meeting
			));
		}
	}
}
